//
//  HJSetPasswordController.m
//  KaiChat
//
//  Created by lx on 2023/10/17.
//

#import "HJSetPasswordController.h"
#import "HJWebViewController.h"
#import "HJTitleTextField.h"
#import "HJPasswordTipView.h"
#import "QMUIButton.h"
#import "HJMinePresent.h"

@interface HJSetPasswordController ()

@property (nonatomic, weak) HJPasswordTipView *tipView;
@property (nonatomic, weak) HJTitleTextField *passwordTextField;
@property (nonatomic, weak) HJTitleTextField *comfirmPasswordTextField;
@property (nonatomic, assign) BOOL isAgreement;
@property (nonatomic, weak) HJStyle1Button *nextButton;
@property (nonatomic, assign) CGFloat tipViewH;

@end

@implementation HJSetPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UILabel *textLabel = [UILabel lableWithSuperView:self.view font:UIFontSemiboldMake(20) color:UIColor.tj_333333Color title:@"Create password" textAlignment:NSTextAlignmentLeft];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight + 20);
        make.left.offset(leftRightMargin);
    }];
    
    UILabel *subTextLabel = [UILabel lableWithSuperView:self.view font:UIFontMake(14) color:UIColor.tj_868391Color title:@"Passwords should contain at least 8 letters or numbers and should not be easily guessed" textAlignment:NSTextAlignmentLeft];
    subTextLabel.numberOfLines = 0;
    subTextLabel.attributedText = [subTextLabel.text titleMargin:1 alignment:NSTextAlignmentLeft];
    [subTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textLabel.mas_bottom).offset(3);
        make.left.right.inset(leftRightMargin);
    }];
    
    HJTitleTextField *passwordTextField = [HJTitleTextField addToView:self.view];
    passwordTextField.backgroundColor = UIColor.whiteColor;
    passwordTextField.titleLabel.text = @"Set password";
    passwordTextField.textField.placeholder = @"Minimum 8 characters";
    passwordTextField.secureTextEntry = YES;
    passwordTextField.textField.keyboardType = UIKeyboardTypeASCIICapable;
    passwordTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [passwordTextField.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(subTextLabel.mas_bottom).offset(60);
        make.height.offset(82);
    }];
    
    HJPasswordTipView *tipView = [HJPasswordTipView addToView:self.view];
    tipView.text = @"The password must contain Alphabets + Digits + Special Symbols";
    [tipView.closeButton addTarget:self action:@selector(didClickCloseButton) forControlEvents:UIControlEventTouchUpInside];
    [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwordTextField.mas_bottom).offset(10);
        make.left.right.equalTo(passwordTextField);
    }];
    
    HJTitleTextField *comfirmPasswordTextField = [HJTitleTextField addToView:self.view];
    comfirmPasswordTextField.backgroundColor = UIColor.whiteColor;
    comfirmPasswordTextField.titleLabel.text = @"Confirm Password";
    comfirmPasswordTextField.textField.placeholder = @"Minimum 8 characters";
    comfirmPasswordTextField.secureTextEntry = YES;
    [comfirmPasswordTextField.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    comfirmPasswordTextField.textField.keyboardType = UIKeyboardTypeASCIICapable;
    comfirmPasswordTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [comfirmPasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(tipView.mas_bottom).offset(20);
        make.height.offset(82);
    }];
    
    _tipView = tipView;
    
    HJStyle1Button *nextButton = [HJStyle1Button buttonWithSuperView:self.view font:UIFontMediumMake(14) color:nil title:@"Next"];
    nextButton.enabled = NO;
    [nextButton addTarget:self action:@selector(didClickNextButton:) forControlEvents:UIControlEventTouchUpInside];
    [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.height.offset(buttonHeight);
        make.bottom.inset(10 + TabbarSafeBottomMargin);
    }];
    
    UIView *bottomView = [UIView addToView:self.view];
    
    CGFloat totalW = 0;
    QMUIButton *agreeButton = [QMUIButton buttonWithSuperView:bottomView font:UIFontMake(14) color:UIColor.tj_868391Color title:@"I have read and agree"];
    [agreeButton addTarget:self action:@selector(didClickAgreeButton:) forControlEvents:UIControlEventTouchUpInside];
    [agreeButton setImage:[UIImage imageNamed:@"rectangle"] forState:UIControlStateNormal];
    [agreeButton setImage:[UIImage imageNamed:@"rectangle_select"] forState:UIControlStateSelected];
    [agreeButton setImagePosition:QMUIButtonImagePositionLeft];
    agreeButton.spacingBetweenImageAndTitle = 4;
    [agreeButton sizeToFit];
    CGFloat buttonW = agreeButton.tj_width;
    totalW += buttonW;
    [agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(0);
        make.width.offset(buttonW);
        make.height.offset(25);
    }];
    
    QMUIButton *agreementButton = [QMUIButton buttonWithSuperView:bottomView font:UIFontMake(14) color:UIColor.tj_mainColor title:@"Terms of Service"];
    [agreementButton addTarget:self action:@selector(didClickpPrivacyButton) forControlEvents:UIControlEventTouchUpInside];
    [agreementButton sizeToFit];
    buttonW = agreementButton.tj_width;
    totalW += buttonW;
    [agreementButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.equalTo(agreeButton.mas_right).offset(6);
        make.width.offset(buttonW);
        make.height.offset(20);
    }];
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(totalW);
        make.left.offset(leftRightMargin);
        make.height.offset(20);
        make.bottom.equalTo(nextButton.mas_top).inset(10);
    }];
    
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
    
    _tipViewH = ceil(tipView.tj_height);
    
    _nextButton = nextButton;
    _tipView = tipView;
    _passwordTextField = passwordTextField;
    _comfirmPasswordTextField = comfirmPasswordTextField;
    
    [self loadAgreementData];
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (_passwordTextField.textField.text.length && _comfirmPasswordTextField.textField.text.length && _isAgreement) {
        _nextButton.enabled = YES;
    } else {
        _nextButton.enabled = NO;
    }
}

- (void)didClickCloseButton {
    [self showTip:NO];
}

- (void)didClickAgreeButton:(UIButton *)button {
    button.selected = !button.selected;
    _isAgreement = button.selected;
    
    [self textFieldDidChange:_passwordTextField.textField];
}

- (void)didClickpPrivacyButton {
    HJWebViewController *webViewController = [[HJWebViewController alloc] init];
    webViewController.url = HJUserInfo.agreement;
    [self.navigationController pushViewController:webViewController];
}

- (void)showTip:(BOOL)show {
    [UIView animateWithDuration:0.25 animations:^{
        if (show) {
            [self.tipView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(self.tipViewH);
            }];
            [self.comfirmPasswordTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.inset(leftRightMargin);
                make.top.equalTo(self.tipView.mas_bottom).offset(20);
                make.height.offset(82);
            }];
        } else {
            [self.tipView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(0);
            }];
            [self.comfirmPasswordTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.inset(leftRightMargin);
                make.top.equalTo(self.passwordTextField.mas_bottom).offset(20);
                make.height.offset(82);
            }];
        }
        
        [self.comfirmPasswordTextField.superview setNeedsLayout];
        [self.comfirmPasswordTextField.superview layoutIfNeeded];
    }];
}


- (void)didClickNextButton:(UIButton *)button {
    
    NSString *password = _passwordTextField.textField.text;
    NSString *comfirmPassword = _comfirmPasswordTextField.textField.text;
    
    if (![password isEqualToString:comfirmPassword]) {
        [HJHUD showErrorMessage:@"Passwords entered twice are not the same"];
        return;
    }
    
    [HJHUD showLoadingInView:self.view];
    [HJMinePresent registerWithAccount:_email password:password complete:^(NSError * _Nullable error) {}];
}
 
- (void)loadAgreementData {
    [HJMinePresent getAgreementComplete:nil];
}

- (BOOL)hiddenBackgroundView {
    return NO;
}

@end
